$(document).ready(function() {

    var abuseSuccessMsg = $("#abuseSuccessMsg").val();//message display when user send abuse successfully
    var urlSite = $('#urlSite').val();
    /********************************************************************
     * Hover option post
     */
    $('.option-post-icon').hover(
	    function() {
		$(this).animate(
			{
			    marginTop: "-4px",
			    marginBottom: "6px"
			}, 100)
	    },
	    function() {
		$(this).animate(
			{
			    marginTop: "0px",
			    marginBottom: "0px"
			}, 100)
	    }
    );
    /*
     * form post
     */
    $(document).on('click', '.show-form-post', function() {
	var id = $(this).attr('href');
	$(id).modal();
    });
    /**
     * error form
     */
    $('#error-form').modal();
    $('#success-active').modal();
    /**
     * Add
     */
    $('#more-input-file').click(function() {
	$(this).before('<div class="input-file-added"><input type="file" accept="image/*" name="photo[]"/> <i class="icon-remove remove-input-file"></i></div>');
    });
    /**
     * delete
     */
//    $('.remove-input-file').live('click', function() {
//	$(this).parent().remove();
//    });

    $('audio,video').mediaelementplayer({
	success: function(player, node) {
	    $('#' + node.id + '-mode').html('mode: ' + player.pluginType);
	},
	iPadUseNativeControls: true,
// force iPhone's native controls
	iPhoneUseNativeControls: true,
// force Android's native controls
	AndroidUseNativeControls: true
    });
    /*
     * Comment
     */

    $('.comment-txt').keyup(function(eventObj) {
	var comment = $(this).val();
	if (comment.length < 168) {//user can only type 167 characters
	    //press enter
//	    if (eventObj.keyCode == 13) {
//		var post_id = $(this).attr('post_id');
//		var objTxtComment = $(this);
//		if (comment != '\n') {
//		    $.post(urlSite + 'comment/add', {post_id: post_id, comment: comment}, function(result) {
//			objTxtComment.parent().after(result);
//			objTxtComment.val(' '); //clear value
//			var oTotalComment = $('body').find('#total-comment-' + post_id);
//			var total_comment = oTotalComment.html();
//			total_comment = Number(Number(total_comment) + 1);
//			oTotalComment.html(total_comment);
//		    });
//		}
//	    }
	} else {
	    $(this).val(comment.substring(0, 167)); //cut string when it overflow
	    alert('Number of characters must be less than 168');
	}
    });
    $('.btn-post-comment').click(function() {

	var comment = $('.comment-txt').val();
	$('.comment-txt').val('');//clear text

	var objTxtComment = $('.comment-txt');
	if (comment.length < 168) {//user can only type 167 characters
	    var post_id = $('.comment-txt').attr('post_id');

	    if (comment != '\n' && comment != '') {

		$(this).parent().after('<div class="progress-bar-comment"><img src="' + urlSite + 'progress_bar.gif"></div>');

		$.post(urlSite + 'comment/add', {post_id: post_id, comment: comment}, function(result) {
		    objTxtComment.parent().parent().find(".progress-bar-comment").remove();
		    objTxtComment.parent().after(result);
//		    objTxtComment.val(''); //clear value
		    var oTotalComment = $('body').find('#total-comment-' + post_id);
		    var total_comment = oTotalComment.html();
		    total_comment = Number(Number(total_comment) + 1);

		    oTotalComment.html(total_comment);
		});
	    }
	} else {
	    $(this).val(comment.substring(0, 167)); //cut string when it overflow
	    alert('Number of characters must be less than 168');
	}
    });
    //view more comment
    $('.view-more-comment').click(function() {

	var objThis = $(this);
	var idPost = $(this).attr('idPost');
	var nextPage = $(this).attr('nextPage');
	var inWall = $(this).attr('in-wall');

	$(this).before('<div class="progress-bar-comment"><img src="' + urlSite + 'progress_bar.gif"></div>');
	$.post(urlSite + 'comment/get', {idPost: idPost, nextPage: nextPage, inWall: inWall}, function(result) {
	    //remove progress bar
	    objThis.parent().find(".progress-bar-comment").remove();
	    if (result) {
		objThis.before(result); //add more comments
		objThis.attr('nextPage', Number(nextPage) + 1);
	    } else {
		//when there is no db to get, you can't click on view more
		objThis.remove();
	    }

	});
    });

    /**
     * Send request to invite friend
     */
    $(document).on('click', '.add-friend', function(e) {

	var userReceive = $(this).attr('user-receive');
	$.post(urlSite + 'friend/invite', {userReceive: userReceive}, function(reponse) {
	    if (reponse == 1) {
		$('#add-friend-success').modal();
		$('body').find('.invite-id-' + userReceive).remove();
		//increase total request
		var numberPending = $('#num-pending').html();
		$('#num-pending').html(parseInt(numberPending) + 1);
		$('#num-pending').show();

	    }
	});

    });
    /**
     * Add friend to list
     * user .live because this page friend page use jquery 1.8
     */
    $(document).on('click', '.add-one-friend', function() {
	var oSelf = $(this);
	var userId = $(this).attr('user-id');
	$.post(urlSite + "friend/add_friend", {userId: userId}, function(response) {
	    $('#accept-friend-success').modal();
	    oSelf.parent().parent().remove();

	    //increase total request
	    var numberPending = $('#num-friend').html();
	    $('#num-friend').html(parseInt(numberPending) + 1);
	    $('#num-friend').show();
	});
    });


    $('.select-country-sign-up').change(function() {
	var country = $(this).val();
	if (country.toLowerCase() == 'us') {
	    $('#zip-code').slideDown(500);
	} else {
	    $('#zip-code').slideUp(500);
	}
    });

    /**
     * add more upload file type to form post
     */
    $('.btn-add-addition').click(function() {
	var oSelectType = $(this).parent().find('.select-type-addition');
	var typeOfSelf = $(this).attr('type-post');
	var typeSelect = oSelectType.val();
	//remove option
	oSelectType.find('option[value=' + oSelectType.val() + ']').remove();
	switch (typeSelect) {
	    case 'photo':
		switch (typeOfSelf) {
		    case 'photo':
			$(this).parent().find('.wrap-upload-addition').append('<br><br><label>More Photo</label><input type="file" name="photo[]" accept="image/*"/><input type="file" name="photo[]" accept="image/*"/><input type="file" name="photo[]" accept="image/*"/><input type="file" name="photo[]" accept="image/*"/>');
			break;
		    default:
			$(this).parent().find('.wrap-upload-addition').append('<br><br><label>Photo</label><input type="file" name="photo[]" accept="image/*"/><input type="file" name="photo[]" accept="image/*"/><input type="file" name="photo[]" accept="image/*"/><input type="file" name="photo[]" accept="image/*"/><input type="file" name="photo[]" accept="image/*"/>');
			break;
		}
		break;
	    case 'audio':
		$(this).parent().find('.wrap-upload-addition').append('<br><br><label>Audio</label><input type="file" name="audio" accept="audio/*"/>');
		break;
	    case 'video':
		$(this).parent().find('.wrap-upload-addition').append('<br><br><label>Video</label><input type="file" name="video" accept="video/*"/>');
		break;
	}
    });

    $('.show-enlarge-img').fancybox({
	'transitionIn': 'elastic',
	'transitionOut': 'elastic',
	'autoScale': false,
    });


    /***********************************
     * send msg
     */
    var options_send_msg = {
	//target: '#form-send-request', //Div tag where content info will be loaded in
	url: urlSite + "/mailbox/message/new", //The php file that handles the file that is uploaded	
	beforeSubmit: function() {
	    //load progress img when uploading happens

	    $('#announce').find('.alert').remove();

	    $('#announce').append('<div id="progressbar-send-msg"><img src="' + urlSite + 'progress_bar.gif"></div>');
	},
	success: function(data) {
	    //Here code can be included that needs to be performed if Ajax request was successful	    
	    $('#announce').find('#progressbar-send-msg').remove();

	    var subject = $('#subject-mg').val();
	    var message = $('#message-mg').val();
	    if (subject && message) {
		$('#announce').append('<div class="alert alert-success"><strong>Congratulation!</strong> You have sent mail successfully</div>');
	    } else {
		$('#announce').append('<div class="alert alert-error"><strong>Error!</strong> Please fill subject and message</div>');
	    }
	}
    };
    $(document).on('submit', '#form-send-request', function() {
	$(this).ajaxSubmit(options_send_msg);
	return false;
    });


    $(document).on('click', '.send-abuse', function(e) {
	var link = $(this).attr('link');
	var userId = $(this).attr('user-id');
	var itemId = $(this).attr('item-id');
	var type = $(this).attr('type-id');
	var text = $(this).parent().parent().find('.select-abusing').val();
	var oBoxReport = $(this).parent().parent();
	$(this).remove();
	$.post(urlSite + 'post/reportabuse', {link: link, userId: userId, itemId: itemId, text: text, type: type}, function(response) {
	    if (response == 1) {
		oBoxReport.find('.select-abusing').remove();
		oBoxReport.parent().find('a:first').remove();
		oBoxReport.find('.modal-body').append('<div class="alert alert-success">' + abuseSuccessMsg + '</div>');

	    }
	});
    });


});